<?

/** dzkone query functionality.
 *
 *  This collection of functions (libary) is licensed under GPL2.
 *  See LICENSE or www.gnu.org for more details.
 *
 *  @author Jyry Kuukkanen
 *  $Id: dzkone_query.php,v 1.4 2005/04/19 19:59:03 jyry Exp $
 */


/** Performs a query based on data in TISKI.
 *  @return bool true if query was ok.
 */
function __dzkone__query(&$Kone) {
    include("sodebug.inc");
    soDebug("__dzkone__query enter", SOD_DET);

    $NORMAL_OPS = array("=", ">", "<", ">=", "<=", "<>");

    $Kone->__Soda->clearAllSections();

    /* this is to store arrays of datacols and wherecols and
     * wherevals for soDa */

    $tables = $Kone->__getColsByTable(1);

    /* ready to run -- empty taffel for results */
    $Kone->__Purkki->emptyStore(TAFFEL);
    $Kone->__Soda->setAction(SODA_SELECT);

    /* 1. The first table is the master table and contains the
     * browse key columns to select and return back. */
    $table_keys = $tables->getkeys();
    $mtable_name = $Kone->__Struct->getMasterTableName();
    $qtables = array($mtable_name);
    $msoda = $tables->getitem($mtable_name);
    $mcols = &$msoda["wherecols"];
    $mcount = count($mcols);

    $sort_cols = implode(" ", $Kone->__Struct->__Sorting->getKeys());
    $sort_vals = $Kone->__Struct->__Sorting->getAsArray(1);
    $Kone->__Soda->clearSection(SODA_WHERE);
    $Kone->__Soda->setSection(SODA_ORDER, $sort_cols, $sort_vals);
    $Kone->__Soda->setSection(SODA_DATA, $mcols);

    /* 2. Loop through all columns and add non-empty to the
     * where section. */
    $keys = $Kone->__Purkki->getColNames(TISKI);
    foreach ($keys as $key) {
        $value = $Kone->__Purkki->getStoreItem(TISKI, $key, 0);
        if ($value) {
            $o = count($NORMAL_OPS);
            $op = "";
            while (--$o >= 0) {
                $oplen = strlen($NORMAL_OPS[$o]);
                if (substr($value, 0, $oplen) ==
                    $NORMAL_OPS[$o]) {
                    $op = $NORMAL_OPS[$o];
                    $o = 0;
                    $value = substr($value, $oplen);
                }; // if
            }; // while

            if (!$op) {
                /* 'like' operand? */
                if (soStrPos($value, "*") != -1) {
                    $op = "like";
                    $value = str_replace("*", "%", $value);
                } else {
                    $op = "=";
                }; // else if 'like'
            }; // else if normal_ops

            /* if key is missing :datatype def, then add it */
            if (!count(soSplitStr($key, ":", 1))) {
                $key = "$key:".$Kone->__Struct->getDatatype($key);
            }; // if
            $Kone->__Soda->addToSection(SODA_WHERE, $key, $value);
            $Kone->__Soda->addToSection(SODA_WHERE_OPS, $key, $op);
            $table_name = soExtractVal($key, ".", 1);
            if (!in_array($table_name, $qtables)) {
                $qtables[] = $table_name;
            }; // if !in_array
        }; // if $value

    }; // foreach column

    /* 3. All tables used in query must be looped and add to the where
    * section to get subtable.keycol = mastertable.keycol deps right. */
    $kcount = count($qtables);
    for ($k = 1; $k < $kcount; $k++) {
        $soda = $tables->getItem($qtables[$k]);
        for ($m = 0; $m < $mcount; $m++) {
            $scols = &$soda["wherecols"];
            $Kone->__Soda->addToSection(SODA_WHERE,
                $scols[$m], SO_DOLLAR.soExtractVal($mcols[$m], ":", 1));
        }; // for $m
    }; // foreach table

    /* run the sql statement for a table */
    $Kone->__Soda->setTables($qtables);
    $result = $Kone->__Soda->run();

    /* handle the result for each table back to columns */
    if ($result) {
        $datacols = $Kone->__Soda->Results->Values->getKeys();
        foreach ($datacols as $datacol) {
            $dcol = soExtractVal($datacol, ":", 1);
            $Kone->__Purkki->setStoreCol(TAFFEL, $dcol,
                $Kone->__Soda->Results->getCol($dcol));
        }; // foreach

        soDebug("dzKone->query found ".$Kone->__Soda->Results->count().
                " rows.", SOD_DET);
        $Kone->__OutItu->setBlockItem(DZI_CONTROL, DZK_RESULT,
                                      $Kone->__Soda->Results->count());
        $list_id = $Kone->__getListFileName($Kone->__Purkki->getStore(TISKI));
        $Kone->__OutItu->setBlockItem(DZI_CONTROL, DZK_STATUS,
                                      $list_id);
        $os = new soOutputStream($list_id,
                                 $Kone->__Soda->Results->Values);
        $result = 1;
    } else {
        soDebug("dzKone->query: error while executing query", SOD_HI);
        $Kone->__OutItu->setBlockItem(DZI_CONTROL, DZK_RESULT,
                                      DZR_QUERY_ERROR);
        $result = 0;
    }; // if $result

    return $result;
} // __dzkone__query




?>
